/*------------------------------------------------------------------------------
 * PROJ : PrototypeWithSpring
 * NAME : sculove.test.xmlParser.KeicXrwHandler.java
 *        
 * Copyright 2009 sculove All rights reserved
 *------------------------------------------------------------------------------
*/
package sculove.test.parser.xml;

import java.util.ArrayList;
import java.util.HashMap;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.xml.sax.Attributes;
import org.xml.sax.SAXException;
import org.xml.sax.SAXParseException;
import org.xml.sax.helpers.DefaultHandler;


/**
 * <pre>
 * sculove.test.xmlParser.KeicXrwHandler.java
 * TODO
 * 
 * </pre>
 * @date : 2009. 9. 25. ���� 3:22:05
 * @version :
 * @author : sculove
 */
public class KeicXrwHandler extends DefaultHandler {
    private Log log = LogFactory.getLog(this.getClass());
    private ArrayList srvList;
    private boolean isInScript = false;
    
    public KeicXrwHandler(ArrayList srvList) {
        super();
        this.srvList = srvList;
    }

    @Override
    public void startElement(String uri, String localName, String qName, Attributes attributes) throws SAXException{
        // script�� ����
        if(qName.equalsIgnoreCase("script")) {
            isInScript = true;
        }
        super.startElement(uri, localName, qName, attributes);
    }

    /* script tag������ ����ó����.
     * @see org.xml.sax.helpers.DefaultHandler#characters(char[], int, int)
     */
    @Override
    public void characters(char[] ch, int start, int length) throws SAXException{
        if(isInScript) {
            StringBuffer content = new StringBuffer();
            boolean isComment = false;
            for(int i=0; i<length; i++) {
                char c=ch[i];
                
                // �ּ�ó������ ��� ���͸� ������ �������� �ٽ�. (/* �ּ��� ó������)
                if(isComment && ( c == '\n' || c == '\r') ) {
                    isComment = false;
                    continue;                    
                }

                //�ּ����� Ȯ�� //�� ���. 
                if( (c == '/') && (i+1 != length) && (ch[i+1] == '/') ) {
                    isComment = true;
                    continue;
                }
                
                if(!isComment) content.append(c);
            }
//            log.info("�ڡڡ� content : " + content.toString());
            
            // Submit ����
            int idx = 0; 
            while( (idx=content.indexOf("cfSubmit")) != -1) {
                int end = content.indexOf(";", idx); 
                
                // cfSubmit ���� �Ϸ�
//                log.info("�ڡڡ� content : " + content.substring(idx, end));
                HashMap info = getSubmitInfo(content.substring(idx, end));
                if(info != null && !isDuplicate(srvList, getKey(info))) srvList.add(info);
                content = content.delete(0, end);
            }
        }
        super.characters(ch, start, length);
    }
    
    @Override
    public void endElement(String uri, String localName, String qName) throws SAXException{
        // script�� ����
        if(qName.equalsIgnoreCase("script")) {
            isInScript = false;
        }
        super.endElement(uri, localName, qName);
    }

    /**
     * submit ���� ����
     * @param str
     * @return
     */
    private HashMap getSubmitInfo(String str) {
        HashMap result = new HashMap();
        String[] strArry = str.split(",");
        for(int i=0; i< strArry.length; i++) {
            strArry[i] = strArry[i].replaceAll("'","").replaceAll("\"","").trim();
            
            if(strArry[i].indexOf("\\") != -1) {
                // script�� ȣ��� ������ ����
                strArry[i] = strArry[i].replace("\\","").trim();
            }
//            log.info("�� " + strArry[i]);
            switch(i) {
                case 1 : result.put("APP", strArry[i]); break;
                case 2 : result.put("SO", strArry[i]); break;                    
                case 3 : result.put("OP", strArry[i]); break;
                case 5 : result.put("INDTO", strArry[i]); break;
                case 6 : result.put("OUTDTO", strArry[i]); break;
            }
        }
        return result;
    }
    
    /**
     * list���� submit �ߺ����� Ȯ��
     * @param list
     * @param key
     * @return
     */
    private boolean isDuplicate(ArrayList list, String key) {
        for(int i=0; i< list.size(); i++) {
            if(key.equals(getKey((HashMap)list.get(i)))) {
                return true;
            }
        }
        return false;
    }
    
    /**
     * hashmap���� submit�� ������ �� �ִ� key ����
     * @param data
     * @return
     */
    private String getKey(HashMap data) {
        return data.get("APP") + "_" + data.get("SO") + "_" + data.get("OP");
    }
}
